package net.cjw.initsb.controller.background;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.entity.Permission;
import net.cjw.initsb.entity.TopicRelation;
import net.cjw.initsb.service.PermissionServiceI;
import net.cjw.initsb.service.TopicRelationServiceI;
@Api(description="关系")
@RestController
@RequestMapping("topic_relation")
public class TopicRelationController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private TopicRelationServiceI topicRelationServiceI;

	@PostMapping("v{version}/create")
	public ServiceResult<String> create(@ApiParam(defaultValue = "1") @PathVariable("version") Integer version,
			@RequestParam String topic_id, @ApiParam(defaultValue = "post") @RequestParam String topic_type,
			@ApiParam(defaultValue = "1") @RequestParam Integer relation_state) {

		String uid = "admin";
		if (Strings.isNullOrEmpty(topic_id) || Strings.isNullOrEmpty(topic_type) || relation_state == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			TopicRelation topicRelation = new TopicRelation();
			topicRelation.setRelationState(relation_state);
			topicRelation.setTopicId(topic_id);
			topicRelation.setTopicType(topic_type);
			return topicRelationServiceI.create(topicRelation);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/update")
	public ServiceResult<String> update(@ApiParam(defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(defaultValue = "1") @RequestParam String id,
			@ApiParam(defaultValue = "1") @RequestParam Integer relation_state) {

		if (Strings.isNullOrEmpty(id) || relation_state == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			TopicRelation topicRelation = new TopicRelation();
			topicRelation.setId(id);
			topicRelation.setRelationState(relation_state);
			return topicRelationServiceI.update(topicRelation);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
